/*
 * @Description: token拦截器
 * @Author: iamsmiling
 * @Date: 2021-06-16 06:36:08
 * @LastEditTime: 2021-06-16 06:46:22
 */

import 'package:credit_business_circle/storage/local_storage.dart';
import 'package:dio/dio.dart';

class TokenInterceptor extends InterceptorsWrapper {
  @override
  onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
    var token = await LocalStorage.read("token");
    options.queryParameters.addAll({"token": token});
    handler.next(options);
  }

  ///可以在这里对token持久化
  @override
  onResponse(Response response, ResponseInterceptorHandler handler) async {
    if (response.realUri.toString().contains("/login")) {
      // var json = jsonDecode(response.data);
      // String token = json["data"] ?? "";
      // if (response.statusCode == 200 && token.isNotEmpty) {
      //   await LocalStorage.save("token", token);
      //   await LocalStorage.save("authed", true);
      // }
    }

    handler.next(response);
  }
}
